package ps.client.plugin.eq2.log;

import java.io.File;
import java.util.Vector;
import java.util.regex.Pattern;


public class LogFileFinder implements Runnable {
	
	public static final Pattern PATTERN_REGEX_LOGFILE = Pattern.compile("eq2log_[a-zA-Z]+\\.txt");
	
	File logPath = null;
    File currentLogFile = null;
	
	Vector<File> logFileList = new Vector<File>();
	
	boolean running = true;
	boolean enable = true;
	
	public LogFileFinder() {
	    this(true);
	}
	
	public LogFileFinder(boolean enabled) {
	    this.enable = enabled;
	    
	    (new Thread(this, "LogFileFinder")).start();
	}
	
	void findLogFilesRecursive(File path) {
		if (path.isFile()) {
			logFileList.add(path);
		} else if (path.isDirectory()) {
			File[] files = path.listFiles();
			if (files != null) {
    			for (int i = 0; i < files.length; i++) {
    				if (files[i].isFile() && PATTERN_REGEX_LOGFILE.matcher(files[i].getName()).matches()) {
    					logFileList.add(files[i]);
    				} else if (files[i].isDirectory()) {
    					findLogFilesRecursive(files[i]);
    				}
    			}
			}
		}
	}
	
	public File[] getLogFiles() {
	    return (File[])logFileList.toArray(new File[logFileList.size()]);
	}
	
	public File getCurrentLogFile() {
		return currentLogFile;
	}
	
	public void setCurrentLogFile(File logFile) {
	    currentLogFile = logFile;
	    fireUsingLogFile(logFile.getName());
	}
	
	public File getLogPath() {
	    return logPath;
	}
	
	public void setLogPath(File logPath) {
	    boolean newLogPath = this.logPath == null || !this.logPath.equals(logPath);
	    
	    if (newLogPath) {
    	    this.logPath = logPath;
    	    fireUsingLogPath(logPath.getAbsolutePath());

	    
    	    boolean state = isEnable();
    	    if (isEnable()) {
    	        setEnable(false);
    	    }
    	    
    	    logFileList.clear();
    	    if (logPath != null) { 
                findLogFilesRecursive(logPath);
                if (logPath.isFile()) {
                    setCurrentLogFile(logPath);
                }
    	    }
            setEnable(state);
	    }
    }
	
	private void searchLastModifiedLogFile() {
	    long lastModified = 0;
        File lastModifiedFile = null;
        for (int i = 0; i < logFileList.size(); i++) {
            if (logFileList.get(i).lastModified() > lastModified) {
                lastModified = logFileList.get(i).lastModified();
                lastModifiedFile = logFileList.get(i);
            }
        }
        if (lastModifiedFile != null && lastModifiedFile != currentLogFile) {
            setCurrentLogFile(lastModifiedFile);
        }
	}
	
	@Override
	public void run() {
		try {
			while (running) {
			    if (enable) {
    				searchLastModifiedLogFile();
			    }
    			Thread.sleep(3000);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void stop() {
		running = false;
	}
	
	public void setEnable(boolean b) {
	    this.enable = b;
	}
	
	public boolean isEnable() {
	    return enable;
	}
	
	public void fireUsingLogFile(String file) {
	}
	
	public void fireUsingLogPath(String path) {
    }
}
